package com.dankegongyu.lib.share;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.lang.ref.WeakReference;

import androidx.annotation.Nullable;

/**
 * @author wpq
 * @version 1.0
 */
class DKUMShareListener implements UMShareListener {

    private String mPlatformName;
    private WeakReference<DKShareDialog> mShareDialogWeakReference;
    private WeakReference<DKShareCallback> mShareCallbackWeakReference;

    DKUMShareListener(String platformName, DKShareDialog shareDialog, DKShareCallback shareCallback) {
        mPlatformName = platformName;
        mShareDialogWeakReference = new WeakReference<>(shareDialog);
        mShareCallbackWeakReference = new WeakReference<>(shareCallback);
    }

    @Nullable
    private DKShareDialog getShareDialog() {
        return mShareDialogWeakReference.get();
    }

    @Nullable
    private Activity getOwnerActivity() {
        DKShareDialog shareDialog = getShareDialog();
        if (shareDialog != null) {
            return shareDialog.getOwnerActivity();
        }
        return null;
    }

    @Nullable
    private DKShareCallback getShareCallback() {
        return mShareCallbackWeakReference.get();
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {
        DKShareDialog shareDialog = getShareDialog();
        if (shareDialog != null) {
            shareDialog.dismiss();
        }

        DKShareCallback shareCallback = getShareCallback();
        if (shareCallback != null) {
            shareCallback.onStart(mPlatformName);
        }
    }

    @Override
    public void onResult(SHARE_MEDIA share_media) {
        // 2018年微信更新了，点分享到微信再回到App不管分享成功还是取消分享都提示成功，所以微信分享只好不提示咯
        if (SHARE_MEDIA.WEIXIN != share_media && SHARE_MEDIA.WEIXIN_CIRCLE != share_media) {
            Activity ownerActivity = getOwnerActivity();
            if (!DKShareUtil.isActivityFinishingOrDestroyed(ownerActivity)) {
                Toast toast = Toast.makeText(ownerActivity, ownerActivity.getString(R.string.dk_share_success), Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }

        DKShareCallback shareCallback = getShareCallback();
        if (shareCallback != null) {
            shareCallback.onResult(mPlatformName, true);
        }
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        Log.e("share error: ", throwable + "");
        Activity ownerActivity = getOwnerActivity();
        if (!DKShareUtil.isActivityFinishingOrDestroyed(ownerActivity)) {
            boolean notInstalled = DKShareUtil.handlePlatformNotInstalled(ownerActivity, share_media);
            if (!notInstalled) {
                if (throwable != null) {
                    Toast.makeText(ownerActivity, ownerActivity.getString(R.string.dk_share_failure) + throwable.getMessage(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(ownerActivity, ownerActivity.getString(R.string.dk_share_failure), Toast.LENGTH_SHORT).show();
                }
            }
        }

        DKShareCallback shareCallback = getShareCallback();
        if (shareCallback != null) {
            shareCallback.onResult(mPlatformName, false);
        }
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
//                Toast.makeText(activity, activity.getString(R.string.dk_share_cancel), Toast.LENGTH_SHORT).show();
        DKShareCallback shareCallback = getShareCallback();
        if (shareCallback != null) {
            shareCallback.onResult(mPlatformName, false);
        }
    }
}
